/**
 * 在connect中预先将数据进行处理组装,
 * 作为一个pure function,将不会带来副作用
 * 避免在每个Test中多次预处理数据
 */
import { partial } from 'lodash';
import { bindActionCreators } from 'redux';
import { message } from 'antd';

// Test Action
import { createTest, updateTest, removeTest, fetchTestDetail } from 'actions/test';

export function mapTestStateToProps(state) {
  const { editTest: testProfile } = state.test;
  const testId = testProfile.id;

  return {
    testId,
    testProfile,
  };
}

export function mapTestDispatchToProps(dispatch, ownProps) {
  const testId = ownProps.testId;
  const lessonId = ownProps.lessonId;
  const levelId = ownProps.levelId;

  const actions = {
    createTest: partial(createTest, lessonId, levelId),
    removeTest: partial(removeTest, lessonId, testId),
    fetchTestDetail: partial(fetchTestDetail, lessonId, testId),
  };

  return {
    ...bindActionCreators(actions, dispatch),
    updateTest(args) {
      return dispatch(updateTest(lessonId, testId, args)).then(result => {
        if (result.code) {
          message.error(result.msg);
        } else {
          message.success('保存成功');
        }
        return Promise.resolve();
      });
    },
  };
}
